package org.example;

import java.util.EmptyStackException;

public class SqStackClass<E>{
    final int initcapacity=10;
    private int capacity;
    private E[] data;
    private int top;
    public SqStackClass(){
        data = (E[]) new Object[initcapacity];
        capacity = initcapacity;
        top = -1;
    }

    private void updatecapacity(int newcapacity){
        E[] newdata = (E[])new Object[newcapacity];
        for(int i =0; i<=top;i++)
             newdata[i]=data[i];
        capacity=newcapacity;
        data=newdata;
    }

    public boolean empty(){
        return top==-1;
    }

    public void push(E e){
        if(top==capacity-1)
            updatecapacity(2*(top+1));
        top++;
        data[top]=e;
    }

    public E pop(){
        if(empty())
            throw new EmptyStackException();
        E e=(E)data[top];
        top--;
        if(capacity>initcapacity&&top+1==capacity/4)
            updatecapacity(capacity/2);
        return e;
    }

    public E peek(){
        if (empty())
            throw new EmptyStackException();
        return (E)data[top];
    }
}


